package com.tkx.dxb.service.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tkx.dxb.common.base.result.R;
import com.tkx.dxb.service.article.entity.Post;
import com.tkx.dxb.service.article.entity.vo.PostQueryVo;
import com.tkx.dxb.service.article.feign.OssFileService;
import com.tkx.dxb.service.article.mapper.PostMapper;
import com.tkx.dxb.service.article.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author victor
 * @since 2021-04-07
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {

    private OssFileService ossFileService;

    @Autowired
    public void setOssFileService(OssFileService ossFileService){
        this.ossFileService = ossFileService;
    }

    @Override
    public List<Map<String, Object>> selectNameListByKey(String key) {
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("title").likeRight("title", key);
        List<Map<String, Object>> list = baseMapper.selectMaps(queryWrapper);
        return list;
    }

    @Override
    public IPage<Post> selectPage(Page<Post> pageParam, PostQueryVo postQueryVo) {
        /**
         * 显示分页查询列表
         * 1.排序：按照sort字段排序
         * 2.分页查询
         * 3.条件查询
         */
        //1
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");

        //2
        if (postQueryVo == null){
            return baseMapper.selectPage(pageParam,queryWrapper);
        }

        //3
        String title = postQueryVo.getTitle();
        String begin = postQueryVo.getTitleDateBegin();
        String end = postQueryVo.getTitleDateEnd();

        if (!StringUtils.isEmpty(title)){
            //左%会使索引失效
            queryWrapper.likeRight("title", title);
        }

        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_modified", begin);
        }

        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_modified", end);
        }

        return baseMapper.selectPage(pageParam,queryWrapper);
    }

    @Override
    public IPage<Post> selectWebPage(Page<Post> pageParam) {
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_modified");
        return baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Cacheable(value = "index", key="'selectHotArticle'")
    @Override
    public List<Post> selectHotArticle() {
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("title_read");
        queryWrapper.last("limit 10");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Post selectById(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    public boolean removeAvatarById(String id) {
        Post post = baseMapper.selectById(id);
        if(post != null) {
            String picture = post.getPicture();
            if(!StringUtils.isEmpty(picture)) {
                // 删除图片
                R r = ossFileService.removeFile(picture);
                return r.getSuccess();
            }
        }
        return false;
    }

    @Override
    public boolean removeAvatarByIds(List<String> idList) {
        List<String> urls = new ArrayList<>();
        for (String id : idList){
            Post post = baseMapper.selectById(id);
            if (post != null){
                String picture = post.getPicture();
                urls.add(picture);
            }
        }
        R r = ossFileService.removeFiles(urls);
        return r.getSuccess();
    }
}
